package com.baoer.controller;

import com.baoer.R;
import com.baoer.util.iconutil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Chen Feiya
 * @CreateTime: 2025-07-08
 * @Description:
 * @Version: 1.0
 */

@RestController
public class CommonController {

    @PostMapping("/uploadFile")
    public R uploadFile(@RequestParam("file") MultipartFile file) {
        String pathtype = "travel/";
        // 调用上传逻辑
        R result = iconutil.uploadFile(file, pathtype);
        if (result.getCode() != 200) {
            return R.er();
        }
        String filename = (String) result.getData();
        // 保存文件信息到数据库、关联数据等
        System.out.println(R.ok("filename:" + filename));
        return result;
    }

//    @PostMapping("/uploadFile")
//    public R uploadFile(@RequestParam("file") MultipartFile[] file) {
//        String pathtype = "travel/";
//        // 调用上传逻辑
//        List<String> filenames = new ArrayList<>();
//
//        for (MultipartFile file1 : file) {
//            if (file1.isEmpty()) continue;
//
//            R result = iconutil.uploadFile(file1, pathtype);
//            if (result.getCode() != 200) {
//                return R.er();
//            }
//
//            String filename = (String) result.getData();
//            filenames.add(filename);
//            System.out.println("上传成功：" + filename);
//        }
//
//        return R.ok(filenames);
//    }

}